# talents_tab.py
import tkinter as tk
from tkinter import messagebox
from talent import * 


class DrawTalent():
    def __init__(self, parent, talent_data, img_save_dir):
        self.width = 900
        self.height = 500
        self.data = talent_data
        self.talent_group_map = talent_data.talent_group_map
        self.img_save_dir = img_save_dir
        # 创建左侧画布
        self.left_talent_pad = tk.Canvas(parent, width = 100, height = 600, bg="black")
        self.left_talent_pad.pack(side="left", padx=10, pady=10, anchor="w")

        # 创建core_talent_pad画布
        self.core_talent_pad = tk.Canvas(parent, width=self.width, height=100, bg="black")
        self.core_talent_pad.pack(side="top", padx=20, pady=20, anchor="n")  # 向顶部靠右显示

        # 创建talent_pad用于绘制圆圈
        self.talent_pad = tk.Canvas(parent, width = self.width, height = self.height, bg="black")  # 画布大小和背景色
        self.talent_pad.pack(side="top", padx=20, pady=20, anchor="n")  # 向顶部靠左显示
        
        self.draw_small_talent = None
        self.draw_core_talent  = None
        # 圆的参数 
        self.talent_switchs = []
        self.radius = 40
        self.circle_positions = [
            (50, 100),  # 圆1的中心坐标
            (50, 250),  # 圆2的中心坐标
            (50, 400)   # 圆3的中心坐标
        ]
        # 绘制圆和绑定事件
        for index, (x, y) in enumerate(self.circle_positions):
            circle = self.left_talent_pad.create_oval(
                x - self.radius, y - self.radius, x + self.radius, y + self.radius,
                fill="lightblue", outline="white", width=2
            )
            self.talent_switchs.append(circle)
            # 绑定鼠标点击事件
            self.left_talent_pad.tag_bind(circle, "<Button-1>", lambda event, idx=index: self.on_circle_click(idx))

        # 绑定窗口大小变化事件
        self.talent_pad.bind("<Configure>", self.on_resize)

    def DrawSmallTalent(self, name):
        data = self.data.talent_data[name]['data']['small_talents_map']
        self.draw_small_talent = SmallTalent(name, data, self.talent_pad, self.img_save_dir, self.width, self.height)
        self.draw_small_talent.draw_small_talent()
        self.draw_small_talent.on_resize(self.event)
    
    def DrawCoreTalent(self, name):
        data = self.data.talent_data[name]['data']['core_talents_map']
        self.draw_core_talent = CoreTalent(name, data, self.core_talent_pad, self.img_save_dir, self.width, 100)
        self.draw_core_talent.draw()
        self.draw_core_talent.on_resize(self.event)
    def on_resize(self, event):
       self.event = event
       if self.draw_small_talent is not None:
          self.draw_small_talent.on_resize(event)
          self.draw_core_talent.on_resize(self.event)

    def on_circle_click(self, index):
        """鼠标点击圆时触发的事件"""
        if(index == 0):
           self.switch_base_talent()
        elif(index == 1):
           print(index)
        else:
            assert(index == 2)
    def switch_base_talent(self):
        """弹出对话框，供用户选择"""
        popup = tk.Toplevel()
        popup.title("选择一个天赋")
        popup.geometry("200x300")

        # 添加一些选择内容
        label = tk.Label(popup, text="请选择一个天赋版块：", font=("Arial", 12))
        label.pack(pady=10)

        # 添加按钮作为选项
        for i in self.talent_group_map:
            btn = tk.Button(popup, text=i, command=lambda i=i: self.select_option(popup, i))
            btn.pack(pady=5)

    def select_option(self, popup, option):
        """选择一个项目后关闭对话框"""
        print(option)
        self.DrawSmallTalent(option)
        self.DrawCoreTalent(option)
        popup.destroy()

class TalentsTab(tk.Frame):
    def __init__(self, parent):
        super().__init__(parent)
        self.talent_group_map = None
        self.talent_circles = None
        self.depence_lines = None
        self.talent_data = TalentData()
        self.create_widgets()

    def create_widgets(self):
        # 在这里添加天赋标签页的控件和布局
        label = tk.Label(self, text="天赋预览")
        label.pack(pady=10)
        DrawTalent(self, self.talent_data, './downloaded_images/')

